package com.mid;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by Lxk on 2019/5/12.
 */
public class Solution94 {

    void inorderVisit(TreeNode node, List<Integer> res) {
        if (node == null) {
            return;
        }
        inorderVisit(node.left, res);
        res.add(node.val);
        inorderVisit(node.right, res);
    }

    /**
     * 二叉树中序遍历递归方式
     *
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        inorderVisit(root, res);
        return res;
    }

    /**
     * 二叉树中序遍历非递归方式
     *
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal2(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (!stack.isEmpty() || cur != null) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                cur = stack.pop();
                res.add(cur.val);
                cur = cur.right;
            }

        }
        return res;
    }
}
